clc
clear
close all

%%
TimeDuration = 0.1;
SampleRate = 200e3;
MasterClockRate = 600e3;
ModulatorOrder = 2;
SamplePerSymbol = 8;

ModulatorConfig.BandwidthTimeProduct = 0.35;

CarrierFrequency = 200e3;
IqImbalanceConfig.A = 3;
IqImbalanceConfig.P = 10;
PhaseNoiseConfig.Level = -50;
PhaseNoiseConfig.FrequencyOffset = 20;
MemoryLessNonlinearityConfig.Method = 'Saleh model';
MemoryLessNonlinearityConfig.InputScaling = 0;
MemoryLessNonlinearityConfig.AMAMParameters = [2.1587 1.1517];
MemoryLessNonlinearityConfig.AMPMParameters =  [4.0033 9.1040];
MemoryLessNonlinearityConfig.OutputScaling = 0;
MemoryLessNonlinearityConfig.ReferenceImpedance = 1;
ThermalNoiseConfig.NoiseTemperature = 290;
AGCConfig.AveragingLength = 256 * 4;
AGCConfig.MaxPowerGain = 400;

source = RandomSource(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol);

x = source();

%% 单天线场景

txRF = TRFSimulator(StartTime=0.2, ...
    SampleRate = SampleRate, ...
    CarrierFrequency=CarrierFrequency, ...
    IqImbalanceConfig=IqImbalanceConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    MasterClockRate = MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig);
rayChannel = Rayleigh(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

ricChannel = Rician(CarrierFrequency=CarrierFrequency, ...
    SampleRate=SampleRate, PathDelays=0, ...
    AveragePathGains=0, MaximumDopplerShift=0);

rxRF = RRFSimulator(StartTime=0, TimeDuration=2, SampleRate=SampleRate, ...
    NumReceiveAntennas=1, CarrierFrequency=200e3, Bandwidth=20e3, ...
    MasterClockRate=MasterClockRate, ...
    MemoryLessNonlinearityConfig=MemoryLessNonlinearityConfig, ...
    ThermalNoiseConfig=ThermalNoiseConfig, ...
    PhaseNoiseConfig=PhaseNoiseConfig, ...
    AGCConfig=AGCConfig, ...
    IqImbalanceConfig=IqImbalanceConfig);

baseBandSignal = GFSK(SampleRate=SampleRate, ...
    TimeDuration=TimeDuration, ...
    ModulatorOrder=ModulatorOrder, ...
    SamplePerSymbol=SamplePerSymbol, ...
    ModulatorConfig=ModulatorConfig);

x1= baseBandSignal(x);
x1 = txRF(x1);
x1 = rayChannel(x1);
y1 = rxRF({x1});


% % 单天线场景
% SampleRate = 200e3;
% TimeDuration = 1;
% ModulatorOrder = 8;
% SamplePerSymbol = 32;
%
%
% ModulatorConfig.BandwidthTimeProduct = 0.35;
% source = RandomSource(SampleRate=SampleRate, ...
%     TimeDuration=TimeDuration, ...
%     ModulatorOrder=ModulatorOrder, ...
%     SamplePerSymbol=SamplePerSymbol);
%
% modualtor = GFSK(SampleRate=SampleRate, ...
%     TimeDuration=TimeDuration, ...
%     ModulatorOrder=ModulatorOrder, ...
%     SamplePerSymbol=SamplePerSymbol, ...
%     ModulatorConfig=ModulatorConfig);
%
% x = source();
% y = modualtor(x);
%
% % Simple test for filter
% M = 2; % Modulator order
% gfskMod = comm.CPMModulator( ...
%     'ModulatorOrder',M, ...
%     'FrequencyPulse','Gaussian', ...
%     'BandwidthTimeProduct',0.5, ...
%     'ModulatorIndex',1, ...
%     'BitInput',false);
% gfskDemod = comm.CPMDemodulator( ...
%     'ModulatorOrder',M, ...
%     'FrequencyPulse','Gaussian', ...
%     'BandwidthTimeProduct',0.5, ...
%     'ModulatorIndex',1, ...
%     'BitOutput',false);
% spf = 10000;        % Symobls per frame
%
%
% data = randi([0 M-1],spf,1);
% const = (-(M-1):2:(M-1))';
% data = const((data(:)+1));
% modSignal = gfskMod(data);
% bw = obw(modSignal, SampleRate);
% y = lowpass(modSignal, bw*0.8, SampleRate, ImpulseResponse="fir", Steepness=0.99);
% receivedData = gfskDemod(modSignal);
% delay = finddelay(data,receivedData);
% errorRate = comm.ErrorRate( ...
%     ReceiveDelay=delay);
% isequal(data(1:end-delay),receivedData(delay+1:end))
% errorStats = errorRate(data,receivedData);
%
% receivedData1 = gfskDemod(y);
% errorStats1 = errorRate(data,receivedData1);